{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fe210be3-eb3e-4b65-9cde-30cdfa231806",
   "metadata": {},
   "source": [
    "# 赛事背景\n",
    "\n",
    "房屋租赁市场是房地产市场的重要组成部分。中国城市化进程的加剧，导致一二线城市房价不断攀升，越来越多的人选择以房屋租赁的方式来满足住房的需求。价格是反映一定时期内房屋租赁价格水平变动趋势和变动程度，分析预测房屋租赁价格，对于发展完善房屋租赁市场有着重要的意义。\n",
    "\n",
    "据统计，中国有近2亿租房人口，租户偏好千变万化，房源种类各不相同。如何高效且合理的解决房屋价值预估，成为各大平台关注的问题。某租房平台将部分房屋信息数据开放，诚邀大家帮助他们建立价格预测模型来预测房屋租赁价格（敏感信息已脱敏）。\n",
    "\n",
    "## 赛事任务\n",
    "\n",
    "给定某租房平台实际业务中的相关租房信息，包含31个与房屋相关的字段，其中“房屋租金”字段为房屋信息的真实基本租金，即不包含服务费、电费、水费和燃气费等。任务目标是通过训练集训练模型，来预测测试集中“房屋租金”字段的具体值，以此为依据，提高房屋价值预估准确率。\n",
    "\n",
    "## 数据说明\n",
    "\n",
    "赛题数据由训练集和测试集组成，总数据量超过30w，包含31个特征字段。为了保证比赛的公平性，将会从中抽取20万条作为训练集，5万条作为测试集，同时会对部分字段信息进行脱敏。\n",
    "\n",
    "```\n",
    "ID、区域1、区域2、区域3、街道、上传日期、服务费、供暖费用、电力基础价格、有阳台、没有停车位、有厨房、有地窖、居住面积、房屋状况、内饰质量、可带宠物、加热类型、有电梯、房屋类型、邮政编码、房间数量、所处楼层、建筑楼层、有花园、最后翻新年份、是新建筑、建成年份、价格趋势、上传图片、房屋租金\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52f648d5-15f5-4014-a53e-cca6c8e49f7f",
   "metadata": {},
   "source": [
    "# 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a92e5edc-407a-4f35-bc32-b68f906efb3b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:29.683461Z",
     "iopub.status.busy": "2022-06-30T08:32:29.683278Z",
     "iopub.status.idle": "2022-06-30T08:32:30.288584Z",
     "shell.execute_reply": "2022-06-30T08:32:30.288048Z",
     "shell.execute_reply.started": "2022-06-30T08:32:29.683442Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73ea5f4d-0a82-4300-a3ec-95070ffbad0c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:30.289871Z",
     "iopub.status.busy": "2022-06-30T08:32:30.289600Z",
     "iopub.status.idle": "2022-06-30T08:32:30.824110Z",
     "shell.execute_reply": "2022-06-30T08:32:30.823198Z",
     "shell.execute_reply.started": "2022-06-30T08:32:30.289853Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df = pd.read_csv('train.csv')\n",
    "test_df = pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dd2a7a67-cfe8-427b-9eaa-4c50c8cc0bc7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:30.825125Z",
     "iopub.status.busy": "2022-06-30T08:32:30.824947Z",
     "iopub.status.idle": "2022-06-30T08:32:30.844521Z",
     "shell.execute_reply": "2022-06-30T08:32:30.844051Z",
     "shell.execute_reply.started": "2022-06-30T08:32:30.825109Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>区域1</th>\n",
       "      <th>区域2</th>\n",
       "      <th>区域3</th>\n",
       "      <th>街道</th>\n",
       "      <th>上传日期</th>\n",
       "      <th>房屋租金</th>\n",
       "      <th>服务费</th>\n",
       "      <th>供暖费用</th>\n",
       "      <th>电力基础价格</th>\n",
       "      <th>...</th>\n",
       "      <th>邮政编码</th>\n",
       "      <th>房间数量</th>\n",
       "      <th>所处楼层</th>\n",
       "      <th>建筑楼层</th>\n",
       "      <th>有花园</th>\n",
       "      <th>最后翻新年份</th>\n",
       "      <th>是新建筑</th>\n",
       "      <th>建成年份</th>\n",
       "      <th>价格趋势</th>\n",
       "      <th>上传图片数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Oct19</td>\n",
       "      <td>569.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>1971.0</td>\n",
       "      <td>4.62</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Oct19</td>\n",
       "      <td>560.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>1967.0</td>\n",
       "      <td>5.95</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>Feb20</td>\n",
       "      <td>720.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>3.83</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>May19</td>\n",
       "      <td>695.0</td>\n",
       "      <td>153.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>1972.0</td>\n",
       "      <td>5.24</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>Oct19</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>350.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>4.5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.63</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  区域1  区域2  区域3  街道   上传日期    房屋租金    服务费  供暖费用  电力基础价格  ...  邮政编码  房间数量  \\\n",
       "0   0    0    0    0   0  Oct19   569.0  140.0   NaN     NaN  ...     0   3.0   \n",
       "1   1    1    1    1   1  Oct19   560.0  120.0   NaN     NaN  ...     1   2.0   \n",
       "2   2    0    2    2   2  Feb20   720.0    NaN   NaN     NaN  ...     2   1.0   \n",
       "3   3    2    3    3   3  May19   695.0  153.0   NaN     NaN  ...     3   2.0   \n",
       "4   4    3    4    4   1  Oct19  1200.0  350.0   NaN     NaN  ...     4   4.5   \n",
       "\n",
       "   所处楼层  建筑楼层    有花园  最后翻新年份   是新建筑    建成年份  价格趋势  上传图片数  \n",
       "0   2.0   3.0  False     NaN  False  1971.0  4.62      6  \n",
       "1   NaN   NaN  False     NaN  False  1967.0  5.95      7  \n",
       "2   1.0   1.0  False     NaN  False  2018.0  3.83     10  \n",
       "3   2.0   3.0  False     NaN  False  1972.0  5.24      7  \n",
       "4   2.0   2.0   True     NaN  False     NaN  5.63     14  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22d63f22-3866-473c-b9bb-86a10215452d",
   "metadata": {},
   "source": [
    "# 数据分析"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e094ffa5-bf3e-4526-b916-933f145e1ab2",
   "metadata": {},
   "source": [
    "## 房屋租金"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f2da0c9-1089-414e-a595-9476a7ebdbbf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:30.845380Z",
     "iopub.status.busy": "2022-06-30T08:32:30.845209Z",
     "iopub.status.idle": "2022-06-30T08:32:30.915545Z",
     "shell.execute_reply": "2022-06-30T08:32:30.914513Z",
     "shell.execute_reply.started": "2022-06-30T08:32:30.845364Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9999999.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df['房屋租金'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "664dcd02-eea5-466f-ab24-794875af6b9c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:30.917680Z",
     "iopub.status.busy": "2022-06-30T08:32:30.917511Z",
     "iopub.status.idle": "2022-06-30T08:32:30.995625Z",
     "shell.execute_reply": "2022-06-30T08:32:30.995156Z",
     "shell.execute_reply.started": "2022-06-30T08:32:30.917665Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>区域1</th>\n",
       "      <th>区域2</th>\n",
       "      <th>区域3</th>\n",
       "      <th>街道</th>\n",
       "      <th>上传日期</th>\n",
       "      <th>房屋租金</th>\n",
       "      <th>服务费</th>\n",
       "      <th>供暖费用</th>\n",
       "      <th>电力基础价格</th>\n",
       "      <th>...</th>\n",
       "      <th>邮政编码</th>\n",
       "      <th>房间数量</th>\n",
       "      <th>所处楼层</th>\n",
       "      <th>建筑楼层</th>\n",
       "      <th>有花园</th>\n",
       "      <th>最后翻新年份</th>\n",
       "      <th>是新建筑</th>\n",
       "      <th>建成年份</th>\n",
       "      <th>价格趋势</th>\n",
       "      <th>上传图片数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1575</th>\n",
       "      <td>1575</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "      <td>268</td>\n",
       "      <td>1</td>\n",
       "      <td>Feb20</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>947</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.58</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ID  区域1  区域2  区域3  街道   上传日期    房屋租金    服务费  供暖费用  电力基础价格  ...  邮政编码  \\\n",
       "1575  1575    9   16  268   1  Feb20  5500.0  500.0   NaN     NaN  ...   947   \n",
       "\n",
       "      房间数量  所处楼层  建筑楼层    有花园  最后翻新年份   是新建筑 建成年份  价格趋势  上传图片数  \n",
       "1575   4.0   NaN   2.0  False     NaN  False  NaN  4.58     13  \n",
       "\n",
       "[1 rows x 31 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df[train_df['房屋租金'] > train_df['房屋租金'].quantile(0.999)].head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b87c9e17-adb0-4b00-9dc2-1ca3acb255ed",
   "metadata": {},
   "source": [
    "## 缺失值分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a4dcff16-b1f6-4605-a9e2-4d288ae2ba00",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:30.996669Z",
     "iopub.status.busy": "2022-06-30T08:32:30.996498Z",
     "iopub.status.idle": "2022-06-30T08:32:31.057848Z",
     "shell.execute_reply": "2022-06-30T08:32:31.057346Z",
     "shell.execute_reply.started": "2022-06-30T08:32:30.996653Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID        0.000000\n",
       "区域1       0.000000\n",
       "区域2       0.000000\n",
       "区域3       0.000000\n",
       "街道        0.000000\n",
       "上传日期      0.000000\n",
       "房屋租金      0.000000\n",
       "服务费       0.025610\n",
       "供暖费用      0.682465\n",
       "电力基础价格    0.825970\n",
       "有阳台       0.000000\n",
       "没有停车位     0.654085\n",
       "有厨房       0.000000\n",
       "有地窖       0.000000\n",
       "居住面积      0.000000\n",
       "房屋状况      0.000015\n",
       "内饰质量      0.007260\n",
       "可带宠物      0.426480\n",
       "加热类型      0.000595\n",
       "有电梯       0.000000\n",
       "房屋类型      0.021005\n",
       "邮政编码      0.000000\n",
       "房间数量      0.000000\n",
       "所处楼层      0.191320\n",
       "建筑楼层      0.363355\n",
       "有花园       0.000000\n",
       "最后翻新年份    0.699170\n",
       "是新建筑      0.000000\n",
       "建成年份      0.212390\n",
       "价格趋势      0.006605\n",
       "上传图片数     0.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.isnull().mean(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26bf7076-fd74-4b3d-a375-346955a08b29",
   "metadata": {},
   "source": [
    "## 相关性分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4013716e-2d7f-4ce3-bf34-0f979fa42103",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.058841Z",
     "iopub.status.busy": "2022-06-30T08:32:31.058666Z",
     "iopub.status.idle": "2022-06-30T08:32:31.490998Z",
     "shell.execute_reply": "2022-06-30T08:32:31.490517Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.058825Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID       -0.000530\n",
       "区域1      -0.002137\n",
       "区域2       0.002115\n",
       "区域3       0.003195\n",
       "街道        0.002761\n",
       "房屋租金      1.000000\n",
       "服务费       0.627498\n",
       "供暖费用      0.114817\n",
       "电力基础价格    0.003633\n",
       "有阳台       0.002828\n",
       "没有停车位     0.004404\n",
       "有厨房       0.004713\n",
       "有地窖      -0.002857\n",
       "居住面积      0.013557\n",
       "房屋状况     -0.001372\n",
       "内饰质量     -0.001297\n",
       "加热类型      0.001421\n",
       "有电梯       0.005293\n",
       "房屋类型      0.000681\n",
       "邮政编码      0.009896\n",
       "房间数量      0.003324\n",
       "所处楼层      0.011161\n",
       "建筑楼层      0.008226\n",
       "有花园      -0.000480\n",
       "最后翻新年份    0.153241\n",
       "是新建筑      0.005062\n",
       "建成年份      0.143588\n",
       "价格趋势      0.010815\n",
       "上传图片数     0.003238\n",
       "Name: 房屋租金, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.corr()['房屋租金']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f416616-51c1-48e5-8ecc-892348f05b25",
   "metadata": {},
   "source": [
    "## 探索性分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "37573743-f27d-4336-bf5f-b944f4e09768",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.491882Z",
     "iopub.status.busy": "2022-06-30T08:32:31.491711Z",
     "iopub.status.idle": "2022-06-30T08:32:31.626781Z",
     "shell.execute_reply": "2022-06-30T08:32:31.626230Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.491866Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='区域1'>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lyz/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 21306 missing from current font.\n",
      "  font.set_text(s, 0.0, flags=flags)\n",
      "/home/lyz/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 22495 missing from current font.\n",
      "  font.set_text(s, 0.0, flags=flags)\n",
      "/home/lyz/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:201: RuntimeWarning: Glyph 21306 missing from current font.\n",
      "  font.set_text(s, 0, flags=flags)\n",
      "/home/lyz/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:201: RuntimeWarning: Glyph 22495 missing from current font.\n",
      "  font.set_text(s, 0, flags=flags)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAEJCAYAAAB2T0usAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAATGElEQVR4nO3dfZBd9V3H8fcHUhBaSwKsKU3QoI3t0DpVmgloHa2iEFptUGmH6khkovlDlPowY6n+kU5bFB1tbVXQaFKhtkWkVbBPNOWh9QnK8iBPKbLlKYkUtk0ALVUb+vWP80t7WXZJ9tzN7iZ5v2bu7Dm/3znf+9u9m/2c8zvn3qSqkCQd3A6Z6wFIkuaeYSBJMgwkSYaBJAnDQJKEYSBJAhbM9QD6OvbYY2vZsmVzPQxJ2m/ccsst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/IX2M/haq/0ZummiPjVve46+I3vW/PU2roeA84Frgb+kO/Jc37Pmm+mOCP+S7kj+3NY+Any2Z817+/TtoebT7Xf++kkeX+1Z8/YJ678D/AvdkXev33eeeeb68HM93zRq/mb7G/Q9A20P9Kk1sP+tNcW4+o6zqg6sMGg/jEPojgx+pj1OoZ1K96y3EfjBKfo+2LPmUuBFU/S9umfNw6doP3bwF3GIn8Pr6C7I7ovX7EjghCFrvBB4JfAqhjhVbrW+ex99ny8GXtyWFwJnASuHrPnyVudlMzTGTwG/NfgzpDsDfgvw6Z417wKWT9G3tWfNLQwc9LS2XwDuBh7qWfPfB5bfOaGv1zRm23f3AdW76M4M7x/yNdoG/EYLmvtp135bX68px6o6sC4gSxpOkkV0d2StBr6tNT9K91lhF1XVzh41z6L7Y/qsj5xPcmZV/UOPmn8AfKqqPj2hfRXwJ1W1vEfNtwN/UAN397X2l9B972dNt+aEOq8HfhtYVlUvGqLO+glNF1fVeJIX0Y2/1x1/hoGkvdL37jlrPqPOEcB3VdVd822choGkvZLk4ar6dmsemDUPuDedSeovyR1TddHz7jlrzv+aYBhIeqbFwOl0t5IOCvCv1jxgaxoGkp7ho3RviLx9YkeSG6x5wNb0moEkyU8tlSRhGEiSMAwkSRgG0j6VZFOSx5LcNddjkZ6LF5ClaUjyNrrPu9r9KasLgBsna6uqtyX5IeC/gcuq6hWzPFxpr3lrqTR9Z1fV4wBJFgK/NkUbVfXZJMtmf4jS9DhNJEkyDCRJhoEkCcNAkoRhIO1TST4E/Bvw0iTbkqyd6zFJk/FuImkfqqo3zfUYpL1hGEjT8xhwWZKvt/VD6P7D88napP2GbzqTJHnNQJJkGEiSMAwkSRgGkiQMA0kS8P83XL+izrtFmQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_df.groupby(['区域1'])['房屋租金'].mean().plot(kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8310c9d2-bd44-4f82-92ea-96d70175c97c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.627698Z",
     "iopub.status.busy": "2022-06-30T08:32:31.627524Z",
     "iopub.status.idle": "2022-06-30T08:32:31.634294Z",
     "shell.execute_reply": "2022-06-30T08:32:31.633913Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.627682Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "有花园\n",
       "False    710.410061\n",
       "True     683.139171\n",
       "Name: 房屋租金, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.groupby(['有花园'])['房屋租金'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb9089c4-f156-4da8-8978-8026104ac989",
   "metadata": {},
   "source": [
    "# 特征编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a8673fa7-a066-4adc-8185-3e4ba0cc4284",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.635107Z",
     "iopub.status.busy": "2022-06-30T08:32:31.634936Z",
     "iopub.status.idle": "2022-06-30T08:32:31.700577Z",
     "shell.execute_reply": "2022-06-30T08:32:31.699583Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.635091Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID          int64\n",
       "区域1         int64\n",
       "区域2         int64\n",
       "区域3         int64\n",
       "街道          int64\n",
       "上传日期       object\n",
       "房屋租金      float64\n",
       "服务费       float64\n",
       "供暖费用      float64\n",
       "电力基础价格    float64\n",
       "有阳台          bool\n",
       "没有停车位     float64\n",
       "有厨房          bool\n",
       "有地窖          bool\n",
       "居住面积      float64\n",
       "房屋状况      float64\n",
       "内饰质量      float64\n",
       "可带宠物       object\n",
       "加热类型      float64\n",
       "有电梯          bool\n",
       "房屋类型      float64\n",
       "邮政编码        int64\n",
       "房间数量      float64\n",
       "所处楼层      float64\n",
       "建筑楼层      float64\n",
       "有花园          bool\n",
       "最后翻新年份    float64\n",
       "是新建筑         bool\n",
       "建成年份      float64\n",
       "价格趋势      float64\n",
       "上传图片数       int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5861eae1-c247-4f1d-9cb7-30989d177384",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.701839Z",
     "iopub.status.busy": "2022-06-30T08:32:31.701661Z",
     "iopub.status.idle": "2022-06-30T08:32:31.859804Z",
     "shell.execute_reply": "2022-06-30T08:32:31.858776Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.701824Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['上传日期_day'] = train_df['上传日期'].apply(lambda x: int(x[-2:]))\n",
    "test_df['上传日期_day'] = test_df['上传日期'].apply(lambda x: int(x[-2:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8bdc4e01-7f5d-4aac-a9ec-32ad91ee5224",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:31.860810Z",
     "iopub.status.busy": "2022-06-30T08:32:31.860576Z",
     "iopub.status.idle": "2022-06-30T08:32:32.005433Z",
     "shell.execute_reply": "2022-06-30T08:32:32.004890Z",
     "shell.execute_reply.started": "2022-06-30T08:32:31.860794Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['上传日期_month'] = train_df['上传日期'].apply(lambda x: x[:2])\n",
    "test_df['上传日期_month'] = test_df['上传日期'].apply(lambda x: x[:2])\n",
    "\n",
    "train_df['上传日期_month'] = train_df['上传日期_month'].apply(lambda x: ['Fe', 'Ma', 'Oc', 'Se'].index(x))\n",
    "test_df['上传日期_month'] = test_df['上传日期_month'].apply(lambda x: ['Fe', 'Ma', 'Oc', 'Se'].index(x))\n",
    "\n",
    "train_df.drop(['上传日期'], axis=1, inplace=True)\n",
    "test_df.drop(['上传日期'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "123864b1-0340-444b-8bed-2ff3ae775ed2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.006348Z",
     "iopub.status.busy": "2022-06-30T08:32:32.006139Z",
     "iopub.status.idle": "2022-06-30T08:32:32.025929Z",
     "shell.execute_reply": "2022-06-30T08:32:32.025435Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.006332Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['可带宠物'] = train_df['可带宠物'].map({'negotiable': 0.5, 'no': 0, 'yea': 1})\n",
    "test_df['可带宠物'] = test_df['可带宠物'].map({'negotiable': 0.5, 'no': 0, 'yea': 1})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ac2b6ddc-bb3e-4b02-862a-c7aa2af50432",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.027340Z",
     "iopub.status.busy": "2022-06-30T08:32:32.027172Z",
     "iopub.status.idle": "2022-06-30T08:32:32.106689Z",
     "shell.execute_reply": "2022-06-30T08:32:32.105617Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.027324Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "for col in train_df.select_dtypes(bool).columns:\n",
    "    train_df[col] = train_df[col].astype(int)\n",
    "    test_df[col] = test_df[col].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4af00af4-a25a-4482-b04e-635fc28cd403",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.107735Z",
     "iopub.status.busy": "2022-06-30T08:32:32.107563Z",
     "iopub.status.idle": "2022-06-30T08:32:32.146953Z",
     "shell.execute_reply": "2022-06-30T08:32:32.146535Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.107719Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID              int64\n",
       "区域1             int64\n",
       "区域2             int64\n",
       "区域3             int64\n",
       "街道              int64\n",
       "房屋租金          float64\n",
       "服务费           float64\n",
       "供暖费用          float64\n",
       "电力基础价格        float64\n",
       "有阳台             int64\n",
       "没有停车位         float64\n",
       "有厨房             int64\n",
       "有地窖             int64\n",
       "居住面积          float64\n",
       "房屋状况          float64\n",
       "内饰质量          float64\n",
       "可带宠物          float64\n",
       "加热类型          float64\n",
       "有电梯             int64\n",
       "房屋类型          float64\n",
       "邮政编码            int64\n",
       "房间数量          float64\n",
       "所处楼层          float64\n",
       "建筑楼层          float64\n",
       "有花园             int64\n",
       "最后翻新年份        float64\n",
       "是新建筑            int64\n",
       "建成年份          float64\n",
       "价格趋势          float64\n",
       "上传图片数           int64\n",
       "上传日期_day        int64\n",
       "上传日期_month      int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b4d6967b-3453-453d-a8b8-21a6b3f6db3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.147736Z",
     "iopub.status.busy": "2022-06-30T08:32:32.147537Z",
     "iopub.status.idle": "2022-06-30T08:32:32.209657Z",
     "shell.execute_reply": "2022-06-30T08:32:32.208610Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.147721Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['房屋租金'] = np.log1p(train_df['房屋租金'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2fd7a8ac-b780-4380-8706-75babafdc32f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.210536Z",
     "iopub.status.busy": "2022-06-30T08:32:32.210340Z",
     "iopub.status.idle": "2022-06-30T08:32:32.298805Z",
     "shell.execute_reply": "2022-06-30T08:32:32.298237Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.210520Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['区域1_租金价格'] = train_df['区域1'].map(train_df.groupby(['区域1'])['房屋租金'].mean())\n",
    "test_df['区域1_租金价格'] = test_df['区域1'].map(train_df.groupby(['区域1'])['房屋租金'].mean())\n",
    "\n",
    "train_df['区域2_租金价格'] = train_df['区域2'].map(train_df.groupby(['区域2'])['房屋租金'].mean())\n",
    "test_df['区域2_租金价格'] = test_df['区域2'].map(train_df.groupby(['区域2'])['房屋租金'].mean())\n",
    "\n",
    "train_df['邮政编码_租金价格'] = train_df['邮政编码'].map(train_df.groupby(['邮政编码'])['房屋租金'].mean())\n",
    "test_df['邮政编码_租金价格'] = test_df['邮政编码'].map(train_df.groupby(['邮政编码'])['房屋租金'].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "78e3ecaf-f075-4d60-afec-c7a8b34c9df1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.299683Z",
     "iopub.status.busy": "2022-06-30T08:32:32.299511Z",
     "iopub.status.idle": "2022-06-30T08:32:32.350564Z",
     "shell.execute_reply": "2022-06-30T08:32:32.349996Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.299667Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['所处楼层_div'] = train_df['所处楼层'] / train_df['建筑楼层']\n",
    "test_df['所处楼层_div'] = test_df['所处楼层'] / test_df['建筑楼层']\n",
    "\n",
    "train_df['所处楼层_max'] = (train_df['所处楼层'] == train_df['建筑楼层']).astype(int)\n",
    "test_df['所处楼层_max'] = (test_df['所处楼层'] == test_df['建筑楼层']).astype(int)\n",
    "\n",
    "train_df['居住面积_mean'] = train_df['居住面积'] / train_df['房间数量']\n",
    "test_df['居住面积_mean'] = test_df['居住面积'] / test_df['房间数量']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1f4eaf2d-b7e5-47ce-8ca7-fddaabe4f244",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.446093Z",
     "iopub.status.busy": "2022-06-30T08:32:32.445557Z",
     "iopub.status.idle": "2022-06-30T08:32:32.514986Z",
     "shell.execute_reply": "2022-06-30T08:32:32.514226Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.446046Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df['建成年龄'] = 2022 - train_df['建成年份']\n",
    "test_df['建成年龄'] = 2022 - test_df['建成年份']\n",
    "\n",
    "train_df['翻新年龄'] = 2022 - train_df['最后翻新年份']\n",
    "test_df['翻新年龄'] = 2022 - test_df['最后翻新年份']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3bdb1d1-4a1e-4d47-a9e0-a044e6f961ee",
   "metadata": {},
   "source": [
    "# 搭建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e8a35789-b1aa-4829-aa28-bd9ffa61def2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.516271Z",
     "iopub.status.busy": "2022-06-30T08:32:32.516019Z",
     "iopub.status.idle": "2022-06-30T08:32:32.653020Z",
     "shell.execute_reply": "2022-06-30T08:32:32.652276Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.516249Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "for col in ['区域1', '区域2', '区域3', '街道', '房屋类型', '房屋状况', '可带宠物', '邮政编码']:\n",
    "    train_df[col] = train_df[col].astype('category')\n",
    "    test_df[col] = test_df[col].astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "772f75ce-ea71-4183-843a-fef5f194905d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:32:32.654084Z",
     "iopub.status.busy": "2022-06-30T08:32:32.653791Z",
     "iopub.status.idle": "2022-06-30T08:32:32.659136Z",
     "shell.execute_reply": "2022-06-30T08:32:32.658705Z",
     "shell.execute_reply.started": "2022-06-30T08:32:32.654066Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 模型交叉验证\n",
    "def run_model_cv(model, kf, X_tr, y, X_te, cate_col=None):\n",
    "    train_pred = np.zeros( len(X_tr) )\n",
    "    test_pred = np.zeros( len(X_te)  )\n",
    "\n",
    "    cv_clf = []\n",
    "    for tr_idx, val_idx in kf.split(X_tr, y):\n",
    "        x_tr = X_tr.iloc[tr_idx]; y_tr = y.iloc[tr_idx]\n",
    "\n",
    "        x_val = X_tr.iloc[val_idx]; y_val = y.iloc[val_idx]\n",
    "\n",
    "        call_back = [\n",
    "            lgb.early_stopping(50),\n",
    "        ]\n",
    "        eval_set = [(x_val, y_val)]\n",
    "        model.fit(x_tr, y_tr, eval_set=eval_set, callbacks=call_back, verbose=-1,\n",
    "                 categorical_feature= 'auto', eval_metric= 'mae', )\n",
    "\n",
    "        cv_clf.append(model)\n",
    "\n",
    "        train_pred[val_idx] = model.predict(x_val)\n",
    "        test_pred += model.predict(X_te)\n",
    "\n",
    "    test_pred /= kf.n_splits\n",
    "    \n",
    "    print(np.abs(np.exp(train_pred) - np.exp(y)).mean())\n",
    "    return train_pred, test_pred, cv_clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8ab27a4f-f93b-4cf1-b592-487646b32b61",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:42:15.262121Z",
     "iopub.status.busy": "2022-06-30T08:42:15.261490Z",
     "iopub.status.idle": "2022-06-30T08:49:17.576632Z",
     "shell.execute_reply": "2022-06-30T08:49:17.575540Z",
     "shell.execute_reply.started": "2022-06-30T08:42:15.262067Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1623]\tvalid_0's l1: 0.111033\tvalid_0's l2: 0.0370112\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1009]\tvalid_0's l1: 0.112559\tvalid_0's l2: 0.0394435\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1981]\tvalid_0's l1: 0.111733\tvalid_0's l2: 0.0417785\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1571]\tvalid_0's l1: 0.111035\tvalid_0's l2: 0.0374154\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[956]\tvalid_0's l1: 0.112443\tvalid_0's l2: 0.0378669\n",
      "132.67942206531757\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1355]\tvalid_0's l1: 0.111478\tvalid_0's l2: 0.0342903\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1812]\tvalid_0's l1: 0.11102\tvalid_0's l2: 0.031588\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1366]\tvalid_0's l1: 0.111805\tvalid_0's l2: 0.0378956\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1634]\tvalid_0's l1: 0.111739\tvalid_0's l2: 0.0434671\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1003]\tvalid_0's l1: 0.113087\tvalid_0's l2: 0.0442706\n",
      "132.7270588835239\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1495]\tvalid_0's l1: 0.111984\tvalid_0's l2: 0.0423403\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1174]\tvalid_0's l1: 0.112207\tvalid_0's l2: 0.0389497\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1443]\tvalid_0's l1: 0.110964\tvalid_0's l2: 0.0354621\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1605]\tvalid_0's l1: 0.110897\tvalid_0's l2: 0.0331678\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1220]\tvalid_0's l1: 0.112779\tvalid_0's l2: 0.0397876\n",
      "132.64623226498279\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1240]\tvalid_0's l1: 0.11159\tvalid_0's l2: 0.0342805\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1559]\tvalid_0's l1: 0.112095\tvalid_0's l2: 0.0404752\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1462]\tvalid_0's l1: 0.111419\tvalid_0's l2: 0.0383801\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[2148]\tvalid_0's l1: 0.109241\tvalid_0's l2: 0.0346719\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[943]\tvalid_0's l1: 0.114262\tvalid_0's l2: 0.0430238\n",
      "132.70114783678866\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1770]\tvalid_0's l1: 0.111326\tvalid_0's l2: 0.0351399\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1686]\tvalid_0's l1: 0.110842\tvalid_0's l2: 0.039807\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1330]\tvalid_0's l1: 0.111908\tvalid_0's l2: 0.038612\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[2007]\tvalid_0's l1: 0.110498\tvalid_0's l2: 0.0344644\n",
      "[LightGBM] [Warning] verbosity is set=-1, verbose=-1 will be ignored. Current value: verbosity=-1\n",
      "[LightGBM] [Warning] Accuracy may be bad since you didn't explicitly set num_leaves OR 2^max_depth > num_leaves. (num_leaves=31).\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[1732]\tvalid_0's l1: 0.11123\tvalid_0's l2: 0.0415788\n",
      "132.1114659580221\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import KFold\n",
    "import lightgbm as lgb\n",
    "\n",
    "test_pred_tta = np.zeros( len(test_df)  )\n",
    "for _ in range(1):\n",
    "    clf = lgb.LGBMRegressor(\n",
    "        max_depth=7, \n",
    "        n_estimators=4000, \n",
    "        n_jobs=-1, \n",
    "        verbose=-1,\n",
    "        verbosity=-1,\n",
    "        learning_rate=0.03,\n",
    "        random_state=np.random.randint(100)\n",
    "    )\n",
    "\n",
    "    train_pred, test_pred, cv_clf = run_model_cv(\n",
    "        clf, KFold(n_splits=5, random_state=np.random.randint(100), shuffle=True),\n",
    "        train_df.drop(['ID', '房屋租金'], axis=1),\n",
    "        train_df['房屋租金'],\n",
    "        test_df.drop(['ID'], axis=1),\n",
    "    )\n",
    "    \n",
    "    test_pred_tta += test_pred\n",
    "\n",
    "# 133.28688613802427，95.7\n",
    "# 132.1 75.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bdf85b57-b07e-4248-bbe9-bbae402f2700",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-30T08:49:27.264233Z",
     "iopub.status.busy": "2022-06-30T08:49:27.263651Z",
     "iopub.status.idle": "2022-06-30T08:49:27.310858Z",
     "shell.execute_reply": "2022-06-30T08:49:27.309803Z",
     "shell.execute_reply.started": "2022-06-30T08:49:27.264183Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "pd.DataFrame({\n",
    "    'ID': test_df['ID'],\n",
    "    '房屋租金': np.exp(test_pred_tta / 1).astype(int)\n",
    "}).to_csv('submit.csv', index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1fa943e-6ffc-4602-a5eb-0c72ca9ebe08",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
